package walkera.cn.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.util.AttributeSet;
import android.view.TextureView;

import java.util.List;

import walkera.cn.bean.CellLocation;
import walkera.cn.config.WkConfig;


public class GameBackgroundView extends TextureView {

    private Paint paintBackgrond ;


    public GameBackgroundView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(); //
    }


    private void init() {
        setSurfaceTextureListener(surfaceTextureListener);
        paintBackgrond = new Paint(Paint.ANTI_ALIAS_FLAG);
    }


    private TextureView.SurfaceTextureListener surfaceTextureListener = new SurfaceTextureListener() {
        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
            initCell();
            drawSomething();
        }

        @Override
        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

        }

        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
            return false;
        }

        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surface) {

        }
    };

    /**列*/
    private int COLUMNS = 30;
    private int ROWS = 0;
    private int cellWidth;
    private void initCell() {
        cellWidth = getWidth() / COLUMNS;
    }

    private synchronized void drawSomething( ) {
        Canvas canvas =  lockCanvas();
        canvas.drawColor(Color.parseColor("#efcc19"));

       List<Point> list = WkConfig.getLocations(COLUMNS) ;
        for(Point point: list){
            int left=point.x *cellWidth;
            int right=left+ cellWidth;
            int top= point.y* cellWidth ;
            int bottom=top + cellWidth;
            CellLocation cellLocation = new CellLocation(left ,right,top,bottom ,Color.RED) ;
            drawTheCell(canvas ,cellLocation);
        }

        // 绘制黑色大框
        paintBackgrond.setColor(Color.parseColor("#000000"));
        paintBackgrond.setStrokeWidth(20);
        canvas.drawLine( 4*cellWidth ,2*cellWidth ,4*cellWidth , 35*cellWidth  ,paintBackgrond);
        canvas.drawLine( (COLUMNS-4)*cellWidth  ,2*cellWidth ,(COLUMNS-4)*cellWidth ,35*cellWidth ,paintBackgrond);
        canvas.drawLine( 4*cellWidth ,35*cellWidth ,(COLUMNS-4)*cellWidth ,35*cellWidth ,paintBackgrond);

        // 绘制文字
        paintBackgrond.setStrokeWidth(5);
        paintBackgrond.setTextSize(100);
        paintBackgrond.setStyle(Paint.Style.FILL);
        String str ="...  俄罗斯方块 ..." ;
        Rect rect = new Rect();
        paintBackgrond.getTextBounds(str, 0, str.length(), rect);
        int strWidth = rect.width();
        canvas.drawText(str ,(getWidth()-strWidth )/2f ,2*cellWidth  ,paintBackgrond  );

         unlockCanvasAndPost(canvas);
    }


    private void drawTheCell(Canvas canvas ,CellLocation cellLocation){
        int padding = 10;
        int ra = 5 ;
        paintBackgrond.setColor(Color.parseColor("#000000"));
        paintBackgrond.setStyle(Paint.Style.FILL);
        paintBackgrond.setStrokeWidth(15);
        // 格子背景
        canvas.drawRoundRect(  cellLocation.getLeft() ,   cellLocation.getTop() ,   cellLocation.getRight() ,   cellLocation.getBottom()  ,ra, ra, paintBackgrond);
        paintBackgrond.setColor(Color.parseColor("#efcc19"));
        paintBackgrond.setStyle(Paint.Style.STROKE);
        paintBackgrond.setStrokeWidth(5);
        //格子内框框
        int left =   cellLocation.getLeft() +  padding  ;
        int right =cellLocation.getRight() -padding ;
        int top = cellLocation.getTop()  +padding ;
        int bottom =cellLocation.getBottom() -padding ;
        canvas.drawRoundRect( left , top , right ,  bottom  ,ra, ra, paintBackgrond);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight() );
    }

}
